package com.jjshop.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jjshop.R;
import com.jjshop.ui.homefragmentitem.HomeItemFragment;
import com.jjshop.utils.GlobalContanst;
import com.jjshop.utils.JsonUtils;
import com.jjshop.utils.PreUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by apple on 18/6/23.
 */

public class HomeFragment extends Fragment {

    private TabLayout tablayout;
    private ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_home,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    private void initData() {
        OkHttpUtils.get().url(GlobalContanst.JJMAIL_INDEX)
                .addHeader(GlobalContanst.COOKIE,GlobalContanst.getCookies(getContext()))
                .addHeader(GlobalContanst.User_Agent,GlobalContanst.getUserAgent(getContext()))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ArrayList<Map<String, String>> listMapByJson = JsonUtils.getListMapByJson(response);
                        if (listMapByJson.size()>0){
                            Map<String, String> map = listMapByJson.get(0);
                            if ("0".equals(map.get("code"))){
                                String data = map.get("data");
                                loadShopId(data);
                                ArrayList<Map<String, String>> listMapByJson1 = JsonUtils.getListMapByJson(data);
                                if (listMapByJson1.size()>0){
                                    Map<String, String> map1 = listMapByJson1.get(0);
                                    String index_data_nav = map1.get("index_data_nav");
                                    ArrayList<Map<String, String>> listMapByJson2 = JsonUtils.getListMapByJson(index_data_nav);
                                    Map<String, String> map2 = listMapByJson2.get(0);
                                    String category_list = map2.get("category_list");
                                    ArrayList<Map<String, String>> listMapByJson3 = JsonUtils.getListMapByJson(category_list);

                                    initTable(listMapByJson3);
                                }
                            }
                        }
                    }
                });
    }

    private void loadShopId(String data) {
        ArrayList<Map<String, String>> listMapByJson1 = JsonUtils.getListMapByJson(data);
        Map<String, String> map1 = listMapByJson1.get(0);
        String index_data = map1.get("index_data");
        ArrayList<Map<String, String>> listMapByJson2 = JsonUtils.getListMapByJson(index_data);
        Map<String, String> map2 = listMapByJson2.get(0);
        String shop_info = map2.get("shop_info");
        ArrayList<Map<String, String>> listMapByJson3 = JsonUtils.getListMapByJson(shop_info);
        Map<String, String> map3 = listMapByJson3.get(0);
//                                "shop_info":{
//                                    "id":"537fdt",
//                                            "shopname":"99严选自营店铺",
//                                            "shopinfo":"99严选自营店铺",
//                                            "shoplogo":"https://pimgds.999d.com/uploads/yx/24/2018-06-12/5b1f6b3385588.png",
//                                            "qrcode_img":"/qrcode/shop/1.png"
//                                },
        String shopid = map3.get("id");
        String shopname = map3.get("shopname");
        String shopinfo = map3.get("shopinfo");
        String shoplogo = map3.get("shoplogo");
        String qrcode_img = map3.get("qrcode_img");

        PreUtils.setString(getContext(),PreUtils.SHOP_ID,shopid);
        PreUtils.setString(getContext(),PreUtils.SHOP_NAME,shopname);
        PreUtils.setString(getContext(),PreUtils.SHOP_INFO,shopinfo);
        PreUtils.setString(getContext(),PreUtils.SHOP_LOGO,shoplogo);
        PreUtils.setString(getContext(),PreUtils.SHOP_IMG,qrcode_img);
    }

    private void initTable(ArrayList<Map<String, String>> listMapByJson3) {
        MyAdapter myAdapter = new MyAdapter(getFragmentManager());
        myAdapter.setData(listMapByJson3);
        viewPager.setAdapter(myAdapter);
        tablayout.setupWithViewPager(viewPager);
    }

    private void initView(View view) {
        tablayout = view.findViewById(R.id.tablayout);
        viewPager = view.findViewById(R.id.viewpager2);
    }

    public static class MyAdapter extends FragmentPagerAdapter{
        private ArrayList<Map<String,String>> datas;
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setData(ArrayList<Map<String,String>> datas){
            this.datas = datas;
        }

        @Override
        public Fragment getItem(int position) {
            Map<String, String> map = datas.get(position);
            String id = map.get("id");
            HomeItemFragment fragment = new HomeItemFragment();
            Bundle bundle = new Bundle();
            bundle.putString("id",id);
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return datas == null ? 0:datas.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position).get("name");
        }
    }
}
